package com.scuec.trainingmanagementsys.controller;

import com.scuec.trainingmanagementsys.entity.DailyReport;
import com.scuec.trainingmanagementsys.service.DailyReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/daily-reports")
@CrossOrigin
public class DailyReportController {

    @Autowired
    private DailyReportService dailyReportService;

    /**
     * 查询所有日报记录
     * @return 包含日报列表的响应
     */
    @GetMapping("/drlist")
    public ResponseEntity<Map<String, Object>> findAll() {
        List<DailyReport> dailyReports = dailyReportService.findAll();
        Map<String, Object> result = new HashMap<>();
        result.put("data", dailyReports);
        result.put("total", dailyReports.size());
        result.put("success", true);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据ID删除日报记录
     * @param dailyReportID 日报ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteById(@PathVariable("id") Integer dailyReportID) {
        Map<String, Object> result = new HashMap<>();
        boolean success = dailyReportService.deleteById(dailyReportID);
        result.put("success", success);
        result.put("message", success ? "删除成功" : "删除失败，ID不存在");
        return ResponseEntity.ok(result);
    }

    /**
     * 插入新的日报记录
     * @param dailyReport 日报实体
     * @return 操作结果
     */
    @PostMapping("/insertdr")
    public ResponseEntity<Map<String, Object>> insert(@RequestBody DailyReport dailyReport) {
        Map<String, Object> result = new HashMap<>();
        boolean success = dailyReportService.insert(dailyReport);
        result.put("success", success);
        result.put("message", success ? "插入成功" : "插入失败");

        if (success) {
            result.put("data", dailyReport);
        }

        return ResponseEntity.ok(result);
    }

    /**
     * 根据学生ID查询日报记录
     * @param studentID 学生ID
     * @return 包含日报列表的响应
     */
    @GetMapping("/drlist/student/{studentID}")
    public ResponseEntity<Map<String, Object>> findByStudentID(@PathVariable("studentID") Integer studentID) {
        List<DailyReport> dailyReports = dailyReportService.findByStudentID(studentID);
        Map<String, Object> result = new HashMap<>();
        result.put("data", dailyReports);
        result.put("total", dailyReports.size());
        result.put("success", true);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据项目ID查询日报记录
     * @param projectID 项目ID
     * @return 包含日报列表的响应
     */
//    @GetMapping("/drlist/project/{projectID}")
//    public ResponseEntity<Map<String, Object>> findByProjectID(@PathVariable("projectID") Integer projectID) {
//        List<DailyReport> dailyReports = dailyReportService.findByProjectID(projectID);
//        Map<String, Object> result = new HashMap<>();
//        result.put("data", dailyReports);
//        result.put("total", dailyReports.size());
//        result.put("success", true);
//        return ResponseEntity.ok(result);
//    }

    /**
     * 查询所有日报记录，关联学生表和团队表
     * @return 包含学生姓名和团队名称的日报记录列表
     */
    @GetMapping("/drlist-with-relations")
    public ResponseEntity<Map<String, Object>> findAllWithRelations() {
        List<Map<String, Object>> dailyReports = dailyReportService.findAllWithRelations();
        Map<String, Object> result = new HashMap<>();
        result.put("data", dailyReports);
        result.put("total", dailyReports.size());
        result.put("success", true);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据学生ID查询日报记录并关联学生表、项目表和团队表
     * @param studentID 学生ID
     * @return 包含学生姓名、项目名称和团队名称的日报记录列表
     */
    @GetMapping("/drlist/student/with-relations/{studentID}")
    public ResponseEntity<Map<String, Object>> findByStudentIDWithRelations(@PathVariable("studentID") Integer studentID) {
        List<Map<String, Object>> dailyReports = dailyReportService.findByStudentIDWithRelations(studentID);
        Map<String, Object> result = new HashMap<>();
        result.put("data", dailyReports);
        result.put("total", dailyReports.size());
        result.put("success", true);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/drlist/project/{projectID}")
    public ResponseEntity<Map<String, Object>> findByProjectID(@PathVariable("projectID") Integer projectID) {
        List<Map<String, Object>> dailyReports = dailyReportService.findByProjectIDWithRelations(projectID);
        Map<String, Object> result = new HashMap<>();
        result.put("data", dailyReports);
        result.put("total", dailyReports.size());
        result.put("success", true);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/count/last7days")
    public ResponseEntity<Map<String, Object>> getDailyReportCountLast7Days() {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> counts = dailyReportService.getDailyCountLast7Days();
        result.put("data", counts);
        result.put("success", true);
        return ResponseEntity.ok(result);
    }
}